import React, { Component } from 'react'
import { connect } from 'react-redux'
import { SwitchButtonsStyle } from 'components/Button'
import { validForm as validFormDuck } from 'ducks'

const classNames = require('classnames')

class SwitchButtons extends Component {
  render() {
    const {
      ops, name, value, actionFieldChange,
    } = this.props
    return (
      <SwitchButtonsStyle>
        {ops.map(button => (
          <label
            className={classNames({
              border: true,
              checked: button.name === value,
            })}
            key={button.name}
            name={button.name}
            onClick={() => actionFieldChange(name, button.name)}
          >
            <span className="radio">
              <input type="radio" className="input" />
              <span className="inner" />
            </span>
            <span className="title">{button.value}</span>
          </label>
        ))}
      </SwitchButtonsStyle>
    )
  }
}

const mapStateToProps = (state, ownProps) => ({
  value: validFormDuck.selectors.fieldValue(state, ownProps.name),
})

const mapDispatchToProps = {
  actionFieldChange: validFormDuck.creators.actionFieldChange,
}

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(SwitchButtons)
